/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pigx.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.DraninageCurveDto;
import com.pig4cloud.pigx.admin.api.entity.OverallSingleholeCurve;
import com.pig4cloud.pigx.admin.api.vo.SingleholeCurveVo;
import com.pig4cloud.pigx.admin.mapper.OverallSingleholeCurveMapper;
import com.pig4cloud.pigx.admin.service.OverallSingleholeCurveService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 四全单底孔开度泄流曲线数据 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-20
 */
@Service
@AllArgsConstructor
public class OverallSingleholeCurveServiceImpl extends ServiceImpl<OverallSingleholeCurveMapper, OverallSingleholeCurve> implements OverallSingleholeCurveService {


    private final OverallSingleholeCurveMapper mapper;


    /**
     * @param res
     * @return SingleholeCurveVo
     * @author lfx
     * @date 2024/8/10 15:31
     * @Desc 查询单底孔泄洪曲线数据
     **/
    @Override
    public SingleholeCurveVo selectCurveData(DraninageCurveDto res) {
        Assert.notNull(res, "参数不能为空");
        List<OverallSingleholeCurve> singleholeCurves = mapper.selectList(Wrappers.lambdaQuery(OverallSingleholeCurve.class)
                .eq(StringUtils.isNotEmpty(res.getReservoirId()), OverallSingleholeCurve::getReservoirId, res.getReservoirId()));
        SingleholeCurveVo singleholeCurveVo = new SingleholeCurveVo();
        if (CollUtil.isNotEmpty(singleholeCurves)) {
            singleholeCurves.stream().collect(Collectors.groupingBy(OverallSingleholeCurve::getAperture))
                    .forEach((k, v) -> {
                        match(k, v, singleholeCurveVo);
                    });
        }
        return singleholeCurveVo;
    }


    /**
     *
     * @param aperture
     * @param singleholeCurves
     * @param singleholeCurveVo
     * @return void
     * @author lfx
     * @date 2024/8/10 15:42
     * @Desc 根据开度匹配数据
     **/
    private void match(Integer aperture, List<OverallSingleholeCurve> singleholeCurves, SingleholeCurveVo singleholeCurveVo){
        switch (aperture) {
            case 1:
                singleholeCurveVo.setOneApertureList(singleholeCurves);
                break;
            case 2:
                singleholeCurveVo.setTwoApertureList(singleholeCurves);
                break;
            case 3:
                singleholeCurveVo.setThreeApertureList(singleholeCurves);
                break;
            case 4:
                singleholeCurveVo.setFourApertureList(singleholeCurves);
                break;
            case 5:
                singleholeCurveVo.setFiveApertureList(singleholeCurves);
                break;
            case 6:
                singleholeCurveVo.setSixApertureList(singleholeCurves);
                break;
            default:
                break;
        }
    }


}
